package com.homedo.common.redis.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;


public class ByteUtils {

    public static Object byteToObject(byte[] bytes) {
        Object obj = null;
        try {
            if (bytes == null) {
                return null;
            }
            ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
            ObjectInputStream ois = new ObjectInputStream(bis);

            obj = ois.readObject();
            bis.close();
            ois.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return obj;
    }

    public static byte[] objectToByte(Object obj) {
        byte[] bytes = null;
        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(bos);
            oos.writeObject(obj);

            bytes = bos.toByteArray();

            bos.close();
            oos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bytes;
    }

	/*public static void main(String[] args) {
        ByteUtils tu = new ByteUtils();
		Test t = new Test();
		t.setId(123);;
		t.setName("adfasfasdf");
		System.out.println(tu.objectToByte(t));
		System.out.println(((Test)tu.byteToObject(tu.objectToByte(t))).getId());
	}*/
}
